package com.vcit.system.service.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.vcit.common.core.domain.entity.SysDept;
import com.vcit.common.utils.DateUtils;
import com.vcit.system.domain.SysNotice;
import com.vcit.system.domain.clazz.TClazz;
import com.vcit.system.domain.clazz.TClazzStudent;
import com.vcit.system.domain.vo.ClazzResultVO;
import com.vcit.system.domain.vo.StudentListVo;
import com.vcit.system.mapper.SysDeptMapper;
import com.vcit.system.mapper.SysNoticeMapper;
import com.vcit.system.mapper.clazz.TClazzMapper;
import com.vcit.system.mapper.clazz.TClazzStudentMapper;
import com.vcit.system.mapper.student.TStudentMapper;
import com.vcit.system.util.WordTemplateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.vcit.system.service.ITClazzStudentService;

/**
 * 班级学生关系Service业务层处理
 * 
 * @author dff
 * @date 2025-01-17
 */
@Service
public class TClazzStudentServiceImpl implements ITClazzStudentService 
{
    @Autowired
    private TClazzStudentMapper tClazzStudentMapper;


    @Autowired
    private TStudentMapper studentMapper;

    @Autowired
    private TClazzMapper clazzMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private SysNoticeMapper sysNoticeMapper;


    @Override
    public ClazzResultVO getClazzResult(Long studentId) {
        StudentListVo student = studentMapper.selectTStudentVoById(studentId);
        TClazzStudent clazzStudent = tClazzStudentMapper.selectTClazzStudentByStudentId(studentId);
        if (clazzStudent.getClazzId() == null||student.getZsStatus()!=8) throw new RuntimeException("未分班");
        TClazz clazz = clazzMapper.selectTClazzById(clazzStudent.getClazzId());
         SysDept sysDept = sysDeptMapper.selectDeptById(student.getDeptId());

        ClazzResultVO vo = new ClazzResultVO();
        vo.setStudentName(student.getStudentName());
        vo.setStudentNo(student.getSno());

       vo.setMajorId(student.getMajorId());
       vo.setMajorName(student.getMajorName());

       vo.setDeptId(sysDept.getDeptId());
       vo.setDeptName(sysDept.getDeptName());

       vo.setClazzId(clazz.getId());
        vo.setClazzName(clazz.getClazzName());
        vo.setZsYear(student.getZsYear());
        vo.setStatus(student.getZsStatus());
        return vo;
    }

    @Override
    public byte[] generateReportCard(Long studentId) throws IOException {
        ClazzResultVO vo = getClazzResult(studentId);
       // return ReportCardWordUtil.generate(vo); world 导出 代码实现

        // 构造参数
        Map<String, String> params = new HashMap<>();
        params.put("studentName", vo.getStudentName());
        //params.put("studentNo", vo.getStudentNo());
        params.put("majorName", vo.getMajorName());
        params.put("clazzName", vo.getClazzName());
        params.put("deptName", vo.getDeptName());
        params.put("zsYear", vo.getZsYear());

        byte[] word = WordTemplateUtil.replaceAndGenerate("templates/report_card_template.docx", params);
        return word;//使用world 文档模板导出
    }
    /**
     * 查询班级学生关系
     * 
     * @param id 班级学生关系主键
     * @return 班级学生关系
     */
    @Override
    public TClazzStudent selectTClazzStudentById(Long id)
    {
        return tClazzStudentMapper.selectTClazzStudentById(id);
    }

    /**
     * 查询班级学生关系列表
     * 
     * @param tClazzStudent 班级学生关系
     * @return 班级学生关系
     */
    @Override
    public List<TClazzStudent> selectTClazzStudentList(TClazzStudent tClazzStudent)
    {
        return tClazzStudentMapper.selectTClazzStudentList(tClazzStudent);
    }

    /**
     * 新增班级学生关系
     * 
     * @param tClazzStudent 班级学生关系
     * @return 结果
     */
    @Override
    public int insertTClazzStudent(TClazzStudent tClazzStudent)
    {
        tClazzStudent.setCreateTime(DateUtils.getNowDate());
        return tClazzStudentMapper.insertTClazzStudent(tClazzStudent);
    }

    /**
     * 修改班级学生关系
     * 
     * @param tClazzStudent 班级学生关系
     * @return 结果
     */
    @Override
    public int updateTClazzStudent(TClazzStudent tClazzStudent)
    {
        tClazzStudent.setUpdateTime(DateUtils.getNowDate());
        return tClazzStudentMapper.updateTClazzStudent(tClazzStudent);
    }

    /**
     * 批量删除班级学生关系
     * 
     * @param ids 需要删除的班级学生关系主键
     * @return 结果
     */
    @Override
    public int deleteTClazzStudentByIds(Long[] ids)
    {
        return tClazzStudentMapper.deleteTClazzStudentByStudentIds(ids);
    }

    /**
     * 删除班级学生关系信息
     * 
     * @param id 班级学生关系主键
     * @return 结果
     */
    @Override
    public int deleteTClazzStudentById(Long id)
    {
        return tClazzStudentMapper.deleteTClazzStudentByStudentId(id);
    }

    @Override
    public SysNotice getClazzResultNoticeDate(StudentListVo student, String noticeType) {
        String year = student.getZsYear();
        SysNotice sysNotice = new SysNotice();
        sysNotice.setNoticeType(noticeType);
        sysNotice.setNoticeTitle(year);
        List<SysNotice> noticeList = sysNoticeMapper.selectNoticeList(sysNotice);
        return noticeList.isEmpty() ? null : noticeList.get(0);
    }
}
